import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.*;
import java.io.File;


final class FontItem extends AbstractAction{
  private final Doer frame;
    JDialog fontDialog=new FontDialog();

    FontItem(String name,Doer frame){
    super(name);
    this.frame=frame;
    String iconFileName="images"+File.separator+name+".gif";
    if(new File(iconFileName).exists())putValue(SMALL_ICON,new ImageIcon(iconFileName));
  }

    public void actionPerformed(ActionEvent e){
      Rectangle bounds=frame.getBounds();
      fontDialog.setLocation(bounds.x+bounds.width/3,bounds.y+bounds.height/3);
      fontDialog.setVisible(true);
    }

    //class FontDialog
    final class FontDialog extends JDialog implements ActionListener,ListSelectionListener{
      private Font aFont=new Font("Utopia",Font.PLAIN,12);
      private int fontStyle=aFont.getStyle();
      private int fontSize=aFont.getSize();
      private final JButton ok,cancel;
      private final JList fontList;
      private final JLabel fontDisplay;
      private final JComboBox chooseSize;

      FontDialog(){
        super(frame,"Font Seletion",true);

	setResizable(false);
        JPanel buttonPane=new JPanel();
        buttonPane.add(ok=createButton("OK"));
        buttonPane.add(cancel=createButton("Cancel"));
        getContentPane().add(buttonPane,BorderLayout.SOUTH);
	//dataPane
	//label"choose a font"
	JPanel dataPane=new JPanel();
	dataPane.setBorder(BorderFactory.createCompoundBorder(
	                     BorderFactory.createLineBorder(Color.BLACK,1),
			     BorderFactory.createEmptyBorder(5,5,5,5)
	                   ));
	GridBagLayout gbLayout=new GridBagLayout();
	dataPane.setLayout(gbLayout);
	GridBagConstraints con=new GridBagConstraints();
	JLabel label=new JLabel("Choose a Font");
	con.fill=GridBagConstraints.HORIZONTAL;
	con.gridwidth=GridBagConstraints.REMAINDER;
	gbLayout.setConstraints(label,con);
	dataPane.add(label);
	//font list
	GraphicsEnvironment e=GraphicsEnvironment.getLocalGraphicsEnvironment();
	String[] fontNames=e.getAvailableFontFamilyNames();
	fontList=new JList(fontNames);
	fontList.setValueIsAdjusting(true);
	fontList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
	fontList.setSelectedValue(aFont.getFamily(),true);
	fontList.addListSelectionListener(this);
	JScrollPane chooseFont=new JScrollPane(fontList);
	chooseFont.setMinimumSize(new Dimension(300,300));
	//display
	JPanel display=new JPanel();
	fontDisplay=new JLabel("Sample Size:x X y Y z Z");
	fontDisplay.setPreferredSize(new Dimension(300,100));
	display.add(fontDisplay);
	//split pane
	JSplitPane splitPane=new JSplitPane(JSplitPane.VERTICAL_SPLIT,true,chooseFont,display);
	splitPane.setOneTouchExpandable(true);
	gbLayout.setConstraints(splitPane,con);
	dataPane.add(splitPane);
	//font size
	JPanel sizePane=new JPanel();
	label=new JLabel("Choose Point Size");
	sizePane.add(label);
	String[] sizeList={"8","10","12","14","16","18","20","22"};
	chooseSize=new JComboBox(sizeList);
	chooseSize.setSelectedItem(Integer.toString(fontSize));
	chooseSize.addActionListener(this);
	sizePane.add(chooseSize);
	gbLayout.setConstraints(sizePane,con);
	dataPane.add(sizePane);
	//style
	JPanel stylePane=new JPanel();
	JCheckBox bold=new JCheckBox("Bold",(fontStyle&Font.BOLD)>0);
	JCheckBox italic=new JCheckBox("Italic",(fontStyle&Font.ITALIC)>0);
	bold.addItemListener(new StyleListener(Font.BOLD));
	italic.addItemListener(new StyleListener(Font.ITALIC));
	stylePane.add(bold);
	stylePane.add(italic);
	gbLayout.setConstraints(stylePane,con);
	dataPane.add(stylePane);
	getContentPane().add(dataPane,BorderLayout.CENTER);
	pack();
      }

      JButton createButton(String lable){
        JButton button=new JButton(lable);
        button.setSize(100,500);
        button.addActionListener(this);
        return button;
      }

      //ActionListener
      public void actionPerformed(ActionEvent e){
        Object source=e.getSource();

        if(source==ok){
	  frame.setFont(aFont);
	  frame.getObs().setFont(aFont);
;
	  setVisible(false);
        }else if(source==cancel)setVisible(false);
	else if(source==chooseSize){
	   fontSize=Integer.parseInt((String)chooseSize.getSelectedItem());
	   aFont=aFont.deriveFont((float)fontSize);
	   fontDisplay.setFont(aFont);
	   fontDisplay.repaint();
	}
      }

      //listSelectionListener
      public void valueChanged(ListSelectionEvent e){
        if(e.getValueIsAdjusting()){
	  aFont=new Font((String)fontList.getSelectedValue(),fontStyle,fontSize);
	  fontDisplay.setFont(aFont);
	  fontDisplay.repaint();
	}
      }

      //class styleListenter
       class StyleListener implements ItemListener{
        private int style;

        StyleListener(int style){
	  this.style=style;
	}

	public void itemStateChanged(ItemEvent e){
	  if(e.getStateChange()==ItemEvent.SELECTED)
            fontStyle |=style;
	  else fontStyle &=~style;
	  aFont=aFont.deriveFont(fontStyle);
	  fontDisplay.setFont(aFont);
	  fontDisplay.repaint();
	}
      }
    }
  }
